const express = require('express')
const config = require('./config/index')
const chalk = require('chalk')
const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('./utils/logger')
const expressSession = require('express-session')
const passport = require('passport')
const systemRouter = require('./routes/system')
const usersRouter = require('./routes/sys_users')
const roleRouter = require('./routes/sys_role')
const menuRouter = require('./routes/sys_menu')
const deptRouter = require('./routes/sys_dept')
const app = express()
const port = config.port
app.use(passport.initialize())

require('./utils/passport')(passport)
app.use(logger.netLog())
app.use(expressSession(config.session))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

app.use(systemRouter)
app.use(usersRouter)
app.use(roleRouter)
app.use(menuRouter)
app.use(deptRouter)

app.listen(port, err => {
  if (!err) {
    console.log(chalk.blue(`server run success: http://localhost:${port}`))
  } else {
    console.log(chalk.red(err))
  }
})
